import type { IUserInfo } from "@/types/user";
import { getToken, removeToken, setToken } from "@/utils/auth";
import { defineStore } from "pinia";
import { reactive, ref } from "vue";

// 用户信息仓库
export const useUserStore = defineStore('user', () => {
    // 用户token
    let token: string = getToken()
    // 用户信息
    let userInfo: IUserInfo = reactive<IUserInfo>(JSON.parse(sessionStorage.getItem('userInfo-key')!) || {})
    // 存用户信息
    function setUserInfo(info: IUserInfo) {
        userInfo.email = info.email
        userInfo.nickname = info.nickname
        userInfo.user_pic = info.user_pic
        sessionStorage.setItem('userInfo-key', JSON.stringify(info))
        console.log(info);
    }
    // 存token
    function setUserToken(t: string) {
        // 先清除
        removeToken()
        setToken(t)
        token = t
    }
    return { token, setUserToken, setUserInfo, userInfo }
})